package org.lan.tree;

import java.util.LinkedList;
import java.util.List;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/11/26 11:01 AM
 */
public class LC_257 {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.right = new TreeNode(4);
        System.out.println(new LC_257().binaryTreePaths(root));;
    }

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new LinkedList<>();
        trace(root, list, new StringBuilder());
        return list;
    }

    private void trace(TreeNode node, List<String> list, StringBuilder sb) {
        if (node != null) {
            sb.append("->").append(node.val);
            if (node.right == null && node.left == null) {
                list.add(sb.substring(2));
            }
            if (node.left != null && node.right != null) {
                trace(node.left, list, new StringBuilder(sb));
                trace(node.right, list, new StringBuilder(sb));

            } else if (node.left == null) {
                trace(node.right, list, new StringBuilder(sb));
            } else {
                trace(node.left, list, new StringBuilder(sb));
            }
        }
    }
}
